package com.work.game.home.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.work.game.R;
import com.work.game.home.ui.inter.InterWithdrawalConfig;
import com.work.game.home.ui.model.WithdrawalConfig;

import java.util.List;

public class MoneyGridViewAdapter extends BaseAdapter implements View.OnClickListener {
    private List<WithdrawalConfig> withdrawalConfigList;
    private Context context;
    private InterWithdrawalConfig interWithdrawalConfig;

    public MoneyGridViewAdapter() {
    }

    public MoneyGridViewAdapter(Context context, List<WithdrawalConfig> withdrawalConfigList, InterWithdrawalConfig interWithdrawalConfig) {
        this.withdrawalConfigList = withdrawalConfigList;
        this.context = context;
        this.interWithdrawalConfig = interWithdrawalConfig;
    }

    @Override
    public int getCount() {
        return withdrawalConfigList.size();
    }

    @Override
    public Object getItem(int position) {
        return withdrawalConfigList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_gv_money, parent, false);

        }
        View llClick = convertView.findViewById(R.id.ll_click);
        llClick.setOnClickListener(this);
        WithdrawalConfig withdrawalConfig = withdrawalConfigList.get(position);
        llClick.setTag(position);
        TextView tvMoney = convertView.findViewById(R.id.tv_money);
        TextView tvMake = convertView.findViewById(R.id.tv_make);
        if (position == withdrawalConfig.getPosition()) {
            llClick.setBackgroundResource(R.drawable.radius_orange_kong_2); // 设置选中的背景
            tvMoney.setTextColor(context.getResources().getColor(R.color.orange));
        } else {
            llClick.setBackgroundResource(R.drawable.radius_huise_kong_2); // 设置未选中的背景
            tvMoney.setTextColor(context.getResources().getColor(R.color.black));
        }

        tvMake.setText(withdrawalConfig.getMake());
        tvMoney.setText(withdrawalConfig.getWcMoney() + "元");
        return convertView;
    }

    @Override
    public void onClick(View v) {
        int position = Integer.parseInt(String.valueOf(v.getTag()));
        for (int i = 0; i < withdrawalConfigList.size(); i++) {
            WithdrawalConfig withdrawalConfig = withdrawalConfigList.get(i);
            if (position == i) {
                withdrawalConfig.setPosition(position);
                interWithdrawalConfig.selectPosition(withdrawalConfig);
            } else {
                withdrawalConfig.setPosition(-1);
            }
        }
        notifyDataSetChanged();
    }

}
